//
//  NSAttributedString+THHook.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/14.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSAttributedString+THHook.h"
#import "THDeBugTool.h"
#import "THCrashManager.h"
@implementation NSAttributedString (THHook)
+ (void)load{
#ifdef DEBUG
    return;
#endif
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class cla  = NSClassFromString(@"NSConcreteAttributedString");
               SEL selectors[] = {
                   @selector(initWithString:),
                   @selector(initWithAttributedString:),
                   @selector(initWithString:attributes:),
               };
               SEL hookselectors[] = {
                   @selector(zh_HookinitWithString:),
                   @selector(zh_HookinitWithAttributedString:),
                   @selector(zh_HookinitWithString:attributes:),
               };
               for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
                   SEL originalSelector = selectors[index];
                   SEL swizzledSelector = hookselectors[index];
                   BOOL hook = ExchangeImplementationsInTwoClasses(cla,originalSelector,[NSAttributedString class], swizzledSelector);
                   if (!hook) {
                       NSLog(@"拦截失败");
                   }
               }
    });
}
- (instancetype)zh_HookinitWithString:(NSString *)str{
    id object = nil;
    @try {
        object = [self zh_HookinitWithString:str];
    }@catch (NSException *exception) {//将错误信息上报
        [[THCrashManager shareInstance] reportExp:exception];
    }
    @finally {
        return object;
    }
}
- (instancetype)zh_HookinitWithAttributedString:(NSAttributedString *)attrStr{
    id object = nil;
       @try {
           object = [self zh_HookinitWithAttributedString:attrStr];
       }@catch (NSException *exception) {
         [[THCrashManager shareInstance] reportExp:exception];
       }@finally {
           return object;
       }
}
- (instancetype)zh_HookinitWithString:(NSString *)str
                              attributes:(nullable NSDictionary<NSAttributedStringKey, id> *)attrs{
    id object = nil;
       @try {
           object = [self zh_HookinitWithString:str attributes:attrs];
       }@catch (NSException *exception) {
           [[THCrashManager shareInstance] reportExp:exception];
       }@finally {
           return object;
       }
}
@end
